package com.sjz.vhr.controller.emp;

import com.sjz.vhr.model.*;
import com.sjz.vhr.service.*;
import com.sjz.vhr.utis.POIUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/employee/basic")
public class EmpBasicController {

    @Autowired
    EmployeeService employeeService;
    @Autowired
    NationService nationService;
    @Autowired
    PoliticsstatusService politicsstatusService;
    @Autowired
    JobLevelService jobLevelService;
    @Autowired
    PositionService positionService;
    @Autowired
    DepartmentService departmentService;

    //分页查找员工
    //@RequestParam(defaultValue = "1") 是给默认值

    /****
     普通搜索
    @GetMapping("/")
    public RespPageBean getEmployeeByPage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            String keywords) {
        return employeeService.getEmployeeByPage(page, size,keywords);
    }
     ***/

    /****
     *
     * @param page
     * @param size
     * @param keywords
     * @return
     */
    //分页查找员工
    @GetMapping("/")                      //@RequestParam(defaultValue = "1") 是给默认值
    public RespPageBean getEmployeeByPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size,
                                          Employee employee, Date[] beginDateScope) {
        return employeeService.getEmployeeByPage(page, size, employee,beginDateScope);
    }


    //添加员工
    @PostMapping("/")
    public RespBean addEmp(@RequestBody Employee employee){ //json对象，要加@RequestBody
        if (employeeService.addEmp(employee) == 1){
            return RespBean.ok("添加成功！");
        }
        return RespBean.error("添加失败！");
    }

    //查询所有民族
    @GetMapping("/nations")
    public List<Nation> getAllNations(){
        return nationService.getAllNations();
    }

    //查询政治面貌
    @GetMapping("/politicsstatus")
    public List<Politicsstatus> getAllPoliticsstatus(){
        return politicsstatusService.getAllPoliticsstatus();
    }

    //查询所有职称
    @GetMapping("/joblevels")
    public List<JObLevel> getAllJobLevels(){
        return jobLevelService.getAllJobLevel();
    }

    //查询所有职位
    @GetMapping("/positions")
    public List<Position> getAllPositions(){
        return positionService.getAllPositions();
    }
    //查询工号
    @GetMapping("/maxWorkID")
    public RespBean maxWorkID(){  //format格式化为字符串，长度为8位
        RespBean respBean = RespBean.build().setStatus(200)
                .setObj(String.format("%08d", employeeService.maxWorkID()+1));
        return respBean;
    }


    //查询所有部门
    @GetMapping("/deps")
    public List<Department> getAllDepartments(){
        return  departmentService.getAllDepartments();
    }

    //删除员工
    @DeleteMapping("/{id}")
    public RespBean deleteEmpByEid(@PathVariable Integer id){
        if (employeeService.deleteEmpByEid(id) == 1){
            return RespBean.ok("删除成功！");
        }
        return RespBean.error("删除失败！");
    }

    //更新员工信息
    @PutMapping("/")
    public RespBean updateEmp(@RequestBody Employee employee){
        if (employeeService.updateEmp(employee) == 1){
            return RespBean.ok("更新成功！");
        }
        return RespBean.error("更新失败！");
    }

    /********
     *

    //Excel导出员工数据
    @GetMapping("/export")
    public ResponseEntity<byte[]> exportData(){
        Employee employee = new Employee();
        List<Employee> list = (List<Employee>) employeeService.getEmployeeByPage(null,null,null).getData();
        //把查到的所有员工数据返回
        return POIUtils.employee2Excel(list);
    }
     * @return
     */

    //Excel导出员工数据
    @GetMapping("/export")
    public ResponseEntity<byte[]> exportData(){
        Employee employee = new Employee();
        List<Employee> list = (List<Employee>) employeeService.getEmployeeByPage(null,null,employee,null).getData();
        //把查到的所有员工数据返回
        return POIUtils.employee2Excel(list);
    }


    //文件上传
    @PostMapping("/import")
    public RespBean importData(MultipartFile file) throws IOException {
        //解析数据
        List<Employee> list = POIUtils.excel2Employee(file,nationService.getAllNations(),politicsstatusService.getAllPoliticsstatus(),
                departmentService.getAllDepartmentsWithOutChildren(),positionService.getAllPositions(),jobLevelService.getAllJobLevel());
        //添加Excel中的数据
        if (employeeService.addEmps(list) == list.size()){
            return RespBean.ok("上传成功！");
        }
        return RespBean.error("上传失败！");
    }


}
